package com.smart4s.app.core.positionservice.utils;

import android.util.Log;

import com.smart4s.app.core.positionservice.activity.MainActivity;
import com.smart4s.app.core.positionservice.entity.LightData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C)
 * <p>
 * FileName: JSONUtils
 * <p>
 * Author: zhaozhipeng
 * <p>
 * Date: 2020/5/26 10:34
 * <p>
 * 将集合数据转换成json格式
 */
public class JSONUtils {

    private JSONUtils() {

    }

    static JSONUtils jsonUtils = new JSONUtils();

    public static JSONUtils getInstance() {
        return jsonUtils;
    }

    public static List<LightData> list;
    public JSONArray jsonArray;
    public JSONObject object;
    public JSONObject object1;
    public String jsonString;//保存带集合的json字符串
    public LightData lightData1;

    //将数据集合转换成JSON格式的字符串
    public String changeArrayDateToJson() {

        list = new ArrayList<LightData>();
        //将数据添加到字符中，转换成JSON格式
        lightData1 = new LightData();

        //如果有SP文件将读取的SP文件进行广播发送
        //如果没有SP文件直接读取本地的数据进行广播发送
        if (MainActivity.sp != null) {
            //读取保存的有效数据
            String mapId = MainActivity.sp.getString("mapId", "");
            String mapX = MainActivity.sp.getString("mapX", "");
            String mapY = MainActivity.sp.getString("mapY", "");
            String mapMobileAzi = MainActivity.sp.getString("mapMobileAzi", "");
            int mapFloar = MainActivity.sp.getInt("mapFloar", 0);
            //添加保存数据
            lightData1.setLid(mapId);
            lightData1.setX(mapX);
            lightData1.setY(mapY);
            lightData1.setMobileAzi(mapMobileAzi);
            lightData1.setFloar(mapFloar);
        } else {
            lightData1.setLid(MainActivity.mIdtext.getText().toString());
            lightData1.setX(MainActivity.mXtext.getText().toString());
            lightData1.setY(MainActivity.mYtext.getText().toString());
            lightData1.setMobileAzi(MainActivity.mMoblieAzitext.getText().toString());
            lightData1.setFloar(Integer.parseInt(MainActivity.mFloarText.getText().toString()));
        }
        if (list.size() == 0) {
            list.add(lightData1);
        }

        jsonArray = null;
        object = null;
        jsonArray = new JSONArray();

        object = new JSONObject();
        //遍历数据将数据添加到JSONobject中
        for (int i = 0; i < list.size(); i++) {
            //一个对象用一个JSONobject来装
            object1 = new JSONObject();
            try {
                object1.put("mapLid", list.get(i).getLid());
                object1.put("mapX", list.get(i).getX());
                object1.put("mapY", list.get(i).getX());
                object1.put("mapMobileAzi", list.get(i).getMobileAzi());
                object1.put("mapFloor", list.get(i).getFloar());
                //将JSONobject对象装入JSONArray数据里面
                jsonArray.put(object1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            jsonString = null;
            //吧JSONobject转换成json格式的字符串
            jsonString = object1.toString();
        }
        return jsonString;
    }

}
